import numpy as np
import csv
from datetime import datetime
from time import strftime


'''
Global and Local Empirical Bayes Smoothers with Gamma Model
'''

def getCurTime():
    """
    get current time
    Return value of the date string format(%Y-%m-%d %H:%M:%S)
    """
    format='%Y-%m-%d %H:%M:%S'
    sdate = None
    cdate = datetime.now()
    try:
        sdate = cdate.strftime(format)
    except:
        raise ValueError
    return sdate

def build_data_list(inputCSV):
    sKey = []
    fn = inputCSV
    ra = csv.DictReader(file(fn), dialect="excel")
    
    for record in ra:
        #print record[ra.fieldnames[0]], type(record[ra.fieldnames[-1]])
        for item in ra.fieldnames:
            temp = float(record[item])
            sKey.append(temp)
    sKey = np.array(sKey)
    sKey.shape=(-1,len(ra.fieldnames))
    return sKey



#--------------------------------------------------------------------------
#MAIN

if __name__ == "__main__":
    print '===================================================='
    print "begin at " + getCurTime()

    filepath = 'C:/_DATA/migration89_08/COUNTY Migration/clean/test/min_pop_500thousand/random_LGLR_Apr29.csv'

    output = []
    data = build_data_list(filepath)

    r = 100000
    disCooe = [2,3,4,5,6,7,8,9,10,12,14,16,18,20,25,30,35]

    for i in range(0,len(data[0,:])):
        list = data[:,i]
        list = np.sort(list)
        output.append(list[4])
    #print output
    
    filepath = 'C:/_DATA/migration89_08/COUNTY Migration/clean/test/min_pop_500thousand/flow_measure_large_than_1000_1.csv'

    data = build_data_list(filepath)
    sig = []

    for tempIndex in range(0,len(disCooe)):
        #tempIndex = 9
        
        for item in data:
            if item[1] > disCooe[tempIndex] * r:
                if item[-1] > output[tempIndex]:
                    sig.append(tempIndex)
                    for t in item[1:]:
                        sig.append(int(t))
    sig = np.array(sig)
    sig.shape = (-1,len(data[0,:]))
    print sig
    np.savetxt(filepath[:-4] + '_significance.csv', sig, delimiter=',', fmt = '%i')